<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Clear + Reimport Test with config 2</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Import maps polyfill -->
    <!-- Remove this when import maps will be widely supported -->
    <script async src="https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js"></script>

    <script type="importmap">
        {
            "imports": {
              "threepipe": "./../../dist/index.mjs",
              "@threepipe/webgi-plugins": "https://unpkg.com/@threepipe/webgi-plugins@0.5.6/dist/index.mjs"
            }
        }

    </script>
    <style id="example-style">
        html, body, #canvas-container, #mcanvas {
            width: 100%;
            height: 100%;
            margin: 0;
            overflow: hidden;
        }
    </style>
    <script type="module" src="../examples-utils/global-loading.mjs"></script>
    <script type="module" src="../examples-utils/simple-code-preview.mjs"></script>
    <script id="example-script" type="module">
        import {_testFinish, _testStart, ThreeViewer, timeout, Color, GBufferPlugin, SSAAPlugin, BaseGroundPlugin} from 'threepipe'
        import {SSReflectionPlugin} from '@threepipe/webgi-plugins'

        const viewer = new ThreeViewer({
            canvas: document.getElementById('mcanvas'),
            plugins: [
                GBufferPlugin,
                SSAAPlugin,
                SSReflectionPlugin,
                BaseGroundPlugin
            ]
        })

        async function init() {
            const ground = viewer.getOrAddPluginSync(BaseGroundPlugin)

            viewer.deleteImportedViewerConfigOnLoad = false
            viewer.deleteImportedViewerConfigOnLoadWait = 2000

            viewer.scene.setBackgroundColor('#000000')
            ground.material.color.set('#000000')

            // at this point both ground and background should be black

            const model3 = await viewer.load('https://samples.threepipe.org/demos/classic-watch.glb')

            await timeout(500)

            // at this point both ground and background should be white

            viewer.scene.setBackgroundColor('#000000')
            ground.material.color.set('#000000')
            ground.material.roughness = 0.45;
            ground.material.userData.separateEnvMapIntensity = true
            ground.material.envMapIntensity = 0

            // at this point both ground and background should be white

            console.log(model3.children.length)
            await timeout(500)
            viewer.scene.clearSceneModels()
            await timeout(500)
            const model4 = await viewer.load('https://samples.threepipe.org/demos/classic-watch.glb')

            await timeout(1000)

            // now, ground should be black and background should be white
            // this is because ground material is used by reference when loaded from the config and color is changed. So when its loaded again, the color should remain black

            // if we load once more but with different options it will import the config again and the ground will be white again
            const model5 = await viewer.load('https://samples.threepipe.org/demos/classic-watch.glb', {
                autoCenter: true
            })

            // ground white, background white

        }

        _testStart()
        init().finally(_testFinish)
    </script>
</head>
<body>
<div id="canvas-container">
    <canvas id="mcanvas"></canvas>
</div>

</body>
